package com.yuqing.sentimentapp.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;

/**
 * Created by hqd on 2017/4/10.
 */
public class TipsTwoBtnDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private View rootView;
    private ButtonListener listener;
    private TextView mTvtitle;
    private TextView mTvcontent;
    private TextView mTvcancle;
    private TextView mTvconfirm;

    public TipsTwoBtnDialog(Context context) {
        super(context, R.style.dialog);
        mContext = context;
        init(context);
    }

    public TipsTwoBtnDialog(Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        init(context);
    }

    private void init(Context context) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(true);
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_tips_two_btn, null);
        setContentView(rootView);
        mTvtitle = (TextView) rootView.findViewById(R.id.tv_title);
        mTvcontent = (TextView) rootView.findViewById(R.id.tv_content);
        mTvcancle = (TextView) rootView.findViewById(R.id.tv_cancle);
        mTvconfirm = (TextView) rootView.findViewById(R.id.tv_confirm);
        mTvconfirm.setOnClickListener(this);
        mTvcancle.setOnClickListener(this);
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvtitle.setText(title);
        }
    }

    public void setContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            mTvcontent.setText(content);
        }
    }

    public void setCancle(String cancle) {
        if (!TextUtils.isEmpty(cancle)) {
            mTvcancle.setText(cancle);
        }
    }

    public void setConfirm(String confirm) {
        if (!TextUtils.isEmpty(confirm)) {
            mTvconfirm.setText(confirm);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                listener.buttonOkClick(this);
                cancel();
                break;
            case R.id.tv_cancle:
                listener.buttonCancleClick(this);
                cancel();
                break;
            default:
                break;
        }
    }

    public void setButtonListener(ButtonListener buttonListener) {
        this.listener = buttonListener;
    }

    public interface ButtonListener {
        void buttonOkClick(Dialog dialog);
        void buttonCancleClick(Dialog dialog);
    }

    @Override
    public void show() {
        super.show();
        // 将对话框的大小按屏幕大小的百分比设置
        WindowManager windowManager = ((BaseActivity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int)(display.getWidth() * 0.7); //设置宽度
        getWindow().setAttributes(lp);
    }
}
